import * as echarts from '../../ec-canvas/echarts';

let app = getApp();
let login = require('../../utils/login');
let $api = require('../../config');


function setOption(chart,option,data) {
    var data = data;
    var option = option;

    chart.setOption(option);
    // return chart;
}

Page({
    onShareAppMessage: function (res) {
        return {
            title: '估值易小程序',
            path: '/pages/index/index',
            success: function () { },
            fail: function () { }
        }
    },
    data: {
        ec: {
            // onInit: initChart
        },
        // chartData:{
        //     name:['上证市盈率','深证市盈率','创业板市盈率'],
        //     date:["2016/03/29", "2016/03/30", "2016/03/31", "2016/04/01", "2016/04/04", "2016/04/05", "2016/04/06","2016/04/07", "2016/04/08", "2016/04/09", "2016/04/10", "2016/04/11", "2016/04/12", "2016/04/13"],
        //     data:[[18, 36, 65, 30, 78, 40, 33,28, 76, 55, 20, 78, 80, 93],[12, 50, 51, 35, 70, 30, 20,32, 50, 61, 75, 80, 85, 95],[10, 30, 31, 50, 40, 20, 30,40, 50, 61, 10, 30, 40, 60]]
        // },
        marqText:''
    },

    onReady() {
        this.Component = this.selectComponent('#mychart-dom-area');
        this.marqFuc();
    },
    onShow(){
        let self = this;
        self.getStockOnLine();
        self.getStockPe();
        self.getOption();
    },
    init_one: function (data) {   //初始化第一个图表
        this.Component.init((canvas, width, height) => {
            const chart = echarts.init(canvas, null, {
                width: width,
                height: height
            });
            // var data = data;
            var option = {
                title : {
                    show: true,//默认为true，可以省略
                    text: '估值易小程序',
                    textStyle:{
                        //文字颜色
                        color:'#ddd',
                        //字体风格,'normal','italic','oblique'
                        fontStyle:'normal',
                        //字体粗细 'normal','bold','bolder','lighter',100 | 200 | 300 | 400...
                        fontWeight:'bold',
                        //字体系列
                        // fontFamily:'sans-serif',
                        //字体大小
                        fontSize:18
                    },
                    // subtext: '纯属虚构',
                    x: 'center'
                },
                backgroundColor: '#fff',
                // title: {
                //     top:'1%',
                //     right: 10,
                //     text: '2018年8月，上证A股市盈率：13.36 深圳A股市盈率：22.86',
                //     textStyle: {
                //         fontSize: 12
                //     }
                // },
                color: ["#3366cc", "#fe5867", "#ffa71d"],
                legend: {
                    // data:['上证市盈率','深证指R市盈率','创业板市盈率'],
                    data:data.title,
                    bottom:0,
                    left: 10,
                    // backgroundColor: 'red',
                    z: 100
                },
                grid: {
                    top:'10%',
                    left: '5%',
                    right: '10%',
                    bottom: 80,
                    containLabel: true
                },
                tooltip: {
                    show: true,
                    trigger: 'axis',
                    // axisPointer:{
                    //     show:true,
                    //     axis:'x',
                    //     label:{
                    //         show:true
                    //     }
                    // },
                    formatter: function (resData) {
                        var name = ['上证成指','深证成指','创业板指']
                        var res = resData[0].name + '\n';
                        for (var i = 0, length = resData.length; i < length; i++) {
                            res += name[i] + '：' + resData[i].data;
                            if(i!==length-1){
                                res += '\n'
                            }
                        }
                        return res
                    },
                    position: function (pos, params, el, elRect, size) {
                        var obj = {top: '20%'};
                        obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 30;
                        return obj;
                    },
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: data.xAxis
                    // show: false
                },
                yAxis: {
                    x: 'center',
                    type: 'value',
                    splitLine: {
                        lineStyle: {
                            type: 'dashed'
                        }
                    }
                    // show: false
                },
                series: [{
                    name:data.title[0],
                    type: 'line',
                    smooth: true,
                    // symbol: "none", //去掉圆点
                    showSymbol:false,
                    hoverAnimation:true,
                    data: data.data[0],
                    lineStyle: {
                        normal: {
                            width:1
                        }
                    },
                }, {
                    name:data.title[1],
                    type: 'line',
                    smooth: true,
                    // symbol: "none", //去掉圆点
                    showSymbol:false,
                    hoverAnimation:true,
                    data: data.data[1],
                    lineStyle: {
                        normal: {
                            width:1
                        }
                    },
                }, {
                    name: data.title[2],
                    type: 'line',
                    smooth: true,
                    // symbol: "none", //去掉圆点
                    showSymbol:false,
                    hoverAnimation:true,
                    data: data.data[2],
                    lineStyle: {
                        normal: {
                            width:1
                        }
                    },
                }]
            };

            setOption(chart,option, data)
            this.chart = chart;
            return chart;
        });
    },
    getOption: function () {
        let token = wx.getStorageSync('userInfo').token;
        let self = this;
        wx.request({
            url:$api.api.stockPe,
            method:'GET',
            data: {
                token
            },
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                // chartData:{
                //     name:['上证市盈率','深证市盈率','创业板市盈率'],
                //         date:["2016/03/29", "2016/03/30", "2016/03/31", "2016/04/01", "2016/04/04", "2016/04/05", "2016/04/06","2016/04/07", "2016/04/08", "2016/04/09", "2016/04/10", "2016/04/11", "2016/04/12", "2016/04/13"],
                //         data:[[18, 36, 65, 30, 78, 40, 33,28, 76, 55, 20, 78, 80, 93],[12, 50, 51, 35, 70, 30, 20,32, 50, 61, 75, 80, 85, 95],[10, 30, 31, 50, 40, 20, 30,40, 50, 61, 10, 30, 40, 60]]
                // },
                var data = res.data.data;
                // chartData:{
                //     name:['上证市盈率','深证市盈率','创业板市盈率'],
                //         date:["2016/03/29", "2016/03/30", "2016/03/31", "2016/04/01", "2016/04/04", "2016/04/05", "2016/04/06","2016/04/07", "2016/04/08", "2016/04/09", "2016/04/10", "2016/04/11", "2016/04/12", "2016/04/13"],
                //         data:[[18, 36, 65, 30, 78, 40, 33,28, 76, 55, 20, 78, 80, 93],[12, 50, 51, 35, 70, 30, 20,32, 50, 61, 75, 80, 85, 95],[10, 30, 31, 50, 40, 20, 30,40, 50, 61, 10, 30, 40, 60]]
                // },


                self.init_one(data)
            }
        })
    },
    // 搜索
    toSeach(){
        let self = this;
        this.getSurplusSearch((surplusTotal)=>{
            if(surplusTotal > 0){
                // wx.navigateTo({
                //   url:'../search/search'
                // })
            }else{
                wx.showModal({
                    title: '提示',
                    content: '您的查询次数已经用完,是否需要购买会员?',
                    success: function(res) {
                        if (res.confirm) {
                            wx.switchTab({
                                url:'../pay/pay'
                            })
                        } else if (res.cancel) {
                        }
                    }
                })
            }
        })
    },
    toDetails(e){
        let code = e.currentTarget.dataset.code;
        let isOwn = e.currentTarget.dataset.isown;
        wx.navigateTo({
            // url:`../klineDetail/klineDetail?code=${code}&isOwn=${isOwn}`
            url:`../klineDetail/klineDetail?code=${code}&from=index`
        })
    },
    search(e){
        var val = e.detail.value;
        this.getResult(val);
    },
    /*获取结果*/
    getResult(val){
        let token = wx.getStorageSync('userInfo').token;
        let self = this;
        self.setData({
            showResult:true
        })
        wx.showLoading({
            title: '搜索中',
        })
        wx.request({
            url:$api.api.search,
            method:'GET',
            data: {
                token,
                key:val
            },
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                let data = res.data;
                let searchList = data.data;
                self.setData({
                    searchList
                })
                wx.hideLoading();
            }
        })
    },
    // 取消搜索
    cancelSearch:function(){
        var self = this;
        self.setData({
            showResult:false
        })
    },
    /*
   * 查询次数
   */
    getSurplusSearch(cb){
        let token = wx.getStorageSync('userInfo').token;
        let self = this;
        wx.request({
            url:$api.api.surplusSearch,
            method:'GET',
            data: {
                token
            },
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                let surplusTotal = res.data.data.left_query_times;
                self.setData({
                    surplusSearch:surplusTotal
                })
                cb && cb(surplusTotal)
            }
        })
    },
    // 证券指数
    getStockOnLine(){
        let token = wx.getStorageSync('userInfo').token;
        let self = this;
        wx.request({
            url:$api.api.stockOnLine,
            method:'GET',
            data: {
                token
            },
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                let stokeIndex = res.data.data;
                self.setData({
                    stokeIndex
                })
            }
        })
    },
    // pe
    getStockPe(){
        let token = wx.getStorageSync('userInfo').token;
        let self = this;
        wx.request({
            url:$api.api.stockPe,
            method:'GET',
            data: {
                token
            },
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                let stockList = res.data.data;
                self.setData({
                    stockList
                })
            }
        })
    },
    // 跑马灯
    marqFuc:function () {
        let token = wx.getStorageSync('userInfo').token;
        let self = this;
        wx.request({
            url:$api.api.other,
            method:'GET',
            data: {
                token
            },
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                var notice = res.data.data.notice
                self.setData({
                    marqText:notice
                })
            }
        })
    },
    // 导航栏跳转
    goToUrl:function (e) {
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
            url: url
        })  //接口调用结束的回调函数
    }
});
